#include "OneD2TwoD.h"

template<class V>
OneD2TwoD<V>::OneD2TwoD():mnRows(0),mnColumns(0),mnCapacity(0)
{
}

template<class V>
void OneD2TwoD<V>::set(unsigned int nRow, unsigned int nCol, V val)
{
    if(val == NULL){
        return;
    }

    if(nRow > mnRows  || nRow < 1){
        qDebug("OneD2TwoD::set --> row > mnRows || row < 1");
        return;
    }

    if(nCol > mnColumns || nCol < 1){
        qDebug("OneD2TwoD::set --> col > mnColumns || col < 1");
        return;
    }

    unsigned int index = (nRow - 1) * mnColumns + nCol;

    set(index,val);
}

template<class V>
void OneD2TwoD<V>::set(unsigned int nIndex, V val)
{
    if(nIndex >=1 && nIndex <= mnCapacity){
        mStore[nIndex] = val;
    }
}

template<class V>
V OneD2TwoD<V>::get(unsigned int nRow, unsigned int nCol)
{
    if(nRow > mnRows  || nRow < 1){
        qDebug("OneD2TwoD::get --> row > mnRows || row < 1");
        return 0;
    }

    if(nCol > mnColumns || nCol < 1){
        qDebug("OneD2TwoD::get --> col > mnColumns || col < 1");
        return 0;
    }

    unsigned int index = (nRow - 1) * mnColumns + nCol;

    return get(index);
}

template<class V>
V OneD2TwoD<V>::get(unsigned int nIndex)
{
    if(mStore.contains(nIndex)){
        return mStore[nIndex];
    }

    return 0;
}

template<class V>
void OneD2TwoD<V>::clear()
{
    QList<unsigned int> keys = mStore.keys();
    unsigned int key = 0;

    foreach(key,keys){
        if(mStore[key]){
            delete mStore[key];
        }
    }

    mStore.clear();
}

template<class V>
void OneD2TwoD<V>::setRows(unsigned int nRows)
{
    mnRows = nRows;
    mnCapacity = mnRows * mnColumns;
}

template<class V>
unsigned int OneD2TwoD<V>::rows(){return mnRows;}

template<class V>
void OneD2TwoD<V>::setCols(unsigned int nCols)
{
    mnColumns = nCols;
    mnCapacity = mnRows * mnColumns;
}

template<class V>
void OneD2TwoD<V>::setCapacity(unsigned int nTotal)
{
    mnCapacity = nTotal;
}

template<class V>
unsigned int OneD2TwoD<V>::capacity(){return mnCapacity;}

template<class V>
unsigned int OneD2TwoD<V>::cols(){return mnColumns;}

template<class V>
QList<V> OneD2TwoD<V>::find(QPoint pos)
{
    QList<V> items;
    QList<unsigned int> keys = mStore.keys();
    unsigned int key = 0;

    foreach(key,keys){
        if(mStore[key]->nearToMe(pos)){
            items.append(mStore[key]);
        }
    }

    return items;
}

template<class V>
unsigned int OneD2TwoD<V>::count()
{
    return mStore.count();
}

template<class V>
V OneD2TwoD<V>::left(V current)
{
    uint c = current->column();
    c--;
    return get(current->row(),c);
}

template<class V>
V OneD2TwoD<V>::top(V current)
{
    uint r = current->row();
    r--;
    return get(r,(current->column()));
}

template<class V>
V OneD2TwoD<V>::right(V current)
{
    uint c = current->column();
    c++;
    return get(current->row(),c);
}

template<class V>
V OneD2TwoD<V>::bottom(V current)
{
    uint r = current->row();
    r++;
    return get(r,(current->column()));
}
